﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace CPPEI.Extensions
{
    public static class XmlDocExtension
    {
        #region get

        public static XmlDocument GetDocument(Stream stream)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(stream);

                return xDoc;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 获取文档
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static XmlDocument GetDocument(string filePath)
        {
            if(!System.IO.File.Exists(filePath))
            {
                return null;
            }

            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(filePath);

                return xDoc;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 获取node
        /// </summary>
        /// <param name="xNode"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static XmlNode GetNode(this XmlNode xNode, string name)
        {
            if (xNode == null)
            {
                return null;
            }
            return xNode.SelectSingleNode(name);
        }

        /// <summary>
        /// 获取属性
        /// </summary>
        /// <param name="xNode"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static XmlAttribute GetAttribute(this XmlNode xNode, string name)
        {
            if (xNode == null)
            {
                return null;
            }
            return xNode.Attributes[name];
        }

        public static string GetAttributeValue(this XmlNode xNode, string name)
        {
            if (xNode == null)
            {
                return null;
            }
            XmlAttribute xAttr = xNode.Attributes[name];
            if (xAttr == null)
            {
                return null;
            }
            return xAttr.Value;
        }

        public static XmlNodeList GetNodes(this XmlNode xNode, string name)
        {
            if(xNode == null)
            {
                return null;
            }
            return xNode.SelectNodes(name);
        }

        public static string GetValue(this XmlNode xNode)
        {
            if(xNode == null)
            {
                return null;
            }

            if(xNode is XmlElement)
            {
                return (xNode as XmlElement).InnerText;
            }
            return xNode.Value;
        }
        #endregion

        #region set

        public static XmlNode AppendNode(this XmlNode xNode, string name)
        {
            XmlNode xTarget = null;
            if (xNode is XmlDocument)
            {
                xTarget = (xNode as XmlDocument).CreateNode(XmlNodeType.Element, name, null);
                xNode.AppendChild(xTarget);
            }
            else
            {
                xTarget = xNode.OwnerDocument.CreateNode(XmlNodeType.Element, name, null);
                xNode.AppendChild(xTarget);
            }
            return xTarget;
        }

        /// <summary>
        /// 设置节点，如果节点不存在就创建一个新的
        /// </summary>
        /// <param name="xNode"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static XmlNode SetNode(this XmlNode xNode, string name)
        {
            XmlNode xTarget = xNode.GetNode(name);
            if (xTarget == null)
            {
                if (xNode is XmlDocument)
                {
                    xTarget = (xNode as XmlDocument).CreateNode(XmlNodeType.Element, name, null);
                    xNode.AppendChild(xTarget);
                }
                else
                {
                    xTarget = xNode.OwnerDocument.CreateNode(XmlNodeType.Element, name, null);
                    xNode.AppendChild(xTarget);
                }
            }
            return xTarget;
        }

        public static void SetValue(this XmlNode xNode, string value)
        {
            if(xNode is XmlElement)
            {
                (xNode as XmlElement).InnerText = value;
            }
            else
            {
                xNode.Value = value;
            }
        }

        public static XmlAttribute SetAttribute(this XmlNode xNode, string name, string value)
        {
            XmlAttribute xTarget = xNode.GetAttribute(name);
            if (xTarget == null)
            {
                if (xNode is XmlDocument)
                {
                    xTarget = (xNode as XmlDocument).CreateAttribute(name);
                }
                else
                {
                    xTarget = xNode.OwnerDocument.CreateAttribute(name);
                }
                xNode.Attributes.Append(xTarget);
            }
            xTarget.Value = value;
            return xTarget;
        }
        #endregion
    }
}
